//
//  MySignViewController.m
//  eshow
//
//  Created by Hisoka on 15/11/17.
//  Copyright © 2015年 Hisoka. All rights reserved.
//

#import "MySignViewController.h"


static int textViewLength = 20;

@interface MySignViewController ()<UITextViewDelegate>

@property (nonatomic, strong) UITextView *signTV;
@property (nonatomic, strong) UILabel *signLB;

@end

@implementation MySignViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customizeBackItem];
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setStyle];
    
}

- (void) viewWillDisappear:(BOOL)animated {
    [self textViewDealloc];
}

- (void) setStyle {
    
    self.navigationItem.title = @"编辑个性签名";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存"
                                                                              style:UIBarButtonItemStyleDone
                                                                             target:self
                                                                             action:@selector(setUserInfoAction)];
    self.navigationItem.rightBarButtonItem.tintColor = ORANGE_COLOR;
    self.view.backgroundColor = GRAY_COLOR;
    
    self.signTV = [UITextView newAutoLayoutView];
    [self.view addSubview:_signTV];
    
    [_signTV autoPinToTopLayoutGuideOfViewController:self withInset:20];
    [_signTV autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view];
    [_signTV autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 200)];
    _signTV.backgroundColor = WHITE_COLOR;
    _signTV.returnKeyType = UIReturnKeyDone;
    _signTV.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    _signTV.delegate = self;
    _signTV.font = [UIFont systemFontOfSize:17.0f];
    _signTV.text = [[[TMCache sharedCache] objectForKey:@"userInfo"] objectForKey:@"sign"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChange:) name:@"UITextFieldTextDidChangeNotification" object:_signTV];
    
    self.signLB = [UILabel newAutoLayoutView];
    [self.view addSubview:_signLB];
    
    [_signLB autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:_signTV withOffset:10];
    [_signLB autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:_signTV withOffset:5];
    [_signLB autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 20)];
    _signLB.text = @"请填写个性签名";
    _signLB.textColor = GRAY_COLOR;
    
    if (_signTV.text.length > 0) {
        _signLB.hidden = YES;
    }else{
        _signLB.hidden = NO;
    }
}

- (void) textViewDidChange:(UITextView *)textView {
    
    if (_signTV.text.length > 0) {
        _signLB.hidden = YES;
    }else{
        _signLB.hidden = NO;
    }
    
    NSString *toBeString = textView.text;
    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage];
    
    if ([lang isEqualToString:@"zh-Hans"]) {
        UITextRange *selectRange = [textView markedTextRange];
        UITextPosition *position = [textView positionFromPosition:selectRange.start offset:0];
        if (!position) {
            if (toBeString.length > textViewLength) {
                textView.text = [toBeString substringToIndex:textViewLength];
            }
        }else{
            
        }
    }else{
        if (toBeString.length > textViewLength) {
            textView.text = [toBeString substringToIndex:textViewLength];
        }
    }
    
}

-(void) textViewDealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:@"UITextFieldTextDidChangeNotification"
                                                 object:_signTV];
}

- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }else{
        return YES;
    }
}

#pragma mark - 设置个性签名内容
- (void) setUserInfoAction {
    if (_signTV.text.length <= 0) {
        [SVProgressHUD showInfoWithStatus:@"请填写个性签名"];
        return;
    }
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:[[TMCache sharedCache] objectForKey:@"SSID"] forKey:@"ssid"];
    [parameters setObject:_signTV.text forKey:@"sign"];
    
    [SVProgressHUD show];
    [[HttpManager instance] requestWithMethod:@"User/upProfile"
                                   parameters:parameters
                                      success:^(NSDictionary *result) {
                                          NSDictionary *userInfoTmp = [result objectForKey:@"data"];
                                          NSString *alertText = [userInfoTmp objectForKey:@"info"];
                                          
                                          [[TMCache sharedCache] setObject:userInfoTmp forKey:@"userInfo"];
                                          
                                          [_signTV resignFirstResponder];
                                          
                                          [SVProgressHUD showSuccessWithStatus:alertText maskType:SVProgressHUDMaskTypeBlack];
                                      }
                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                          [SVProgressHUD showInfoWithStatus:[error localizedDescription] maskType:SVProgressHUDMaskTypeBlack];
                                      }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
